
#include <BUniform>
#include "member_BUniform.h"

using namespace BWE;

#define member					(*(member_BUniform*)_ptr)
#define member_allocate()		_ptr = new member_BUniform(this)
#define member_release()		delete (member_BUniform*)_ptr

BUniform::BUniform()
{
	member_allocate();
}
BUniform::BUniform(const BString& sign, Type type, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(type, size);
}
BUniform::BUniform(const BString& sign, int value, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(Type_Int, size);
	int* data = (int*)member.value.data();
	for (int i = 0; i < size; i++)
	{
		data[i] = value;
	}
	member.dirty = true;
}
BUniform::BUniform(const BString& sign, float value, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(Type_Float, size);
	float* data = (float*)member.value.data();
	for (int i = 0; i < size; i++)
	{
		data[i] = value;
	}
	member.dirty = true;
}
BUniform::BUniform(const BString& sign, const BVector& vec, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(Type_Vec3f, size);
	BVector* data = (BVector*)member.value.data();
	for (int i = 0; i < size; i++)
	{
		data[i] = vec;
	}
	member.dirty = true;

}
BUniform::BUniform(const BString& sign, const BColorf& color, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(Type_Vec4f, size);
	BColorf* data = (BColorf*)member.value.data();
	for (int i = 0; i < size; i++)
	{
		data[i] = color;
	}
	member.dirty = true;

}
BUniform::BUniform(const BString& sign, const BMatrix& matrix, int size)
{
	member_allocate();
	member.sign = sign;
	member.init(Type_Matrixf, size);
	BMatrix* data = (BMatrix*)member.value.data();
	for (int i = 0; i < size; i++)
	{
		data[i] = matrix;
	}
	member.dirty = true;
}
BUniform::~BUniform()
{

}

void BUniform::setSign(const BString& sign)
{
	if (member.sign != sign)
	{
		member.sign = sign;
		member.dirty = true;
	}
}
const BString& BUniform::sign() const
{
	return member.sign;
}

void BUniform::setType(Type type)
{
	if (member.type != type)
	{
		member.type = type;
		member.dirty = true;
	}
}
BUniform::Type BUniform::type() const
{
	return member.type;
}

void BUniform::setSize(int size)
{
	if (member.size != size)
	{
		member.size = size;
		member.dirty = true;
	}
}
int BUniform::size() const
{
	return member.size;
}

bool BUniform::set(int value)
{
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		*data = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(float value)
{
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		*data = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BVector& vector)
{
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		*data = vector;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BColorf& color)
{
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		*data = color;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(const BMatrix& matrix)
{
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		*data = matrix;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::set(int pos, int value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		data[pos] = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, float value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		data[pos] = value;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BVector& vector)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		data[pos] = vector;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BColorf& color)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		data[pos] = color;
		member.dirty = true;
		return true;
	}
	return false;
}
bool BUniform::set(int pos, const BMatrix& matrix)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		data[pos] = matrix;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::get(int& value)
{
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		value = *data;
		return true;
	}
	return false;
}
bool BUniform::get(float& value)
{
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		value = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BVector& vector)
{
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		vector = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BColorf& color)
{
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		color = *data;
		return true;
	}
	return false;
}
bool BUniform::get(BMatrix& matrix)
{
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		matrix = *data;
		member.dirty = true;
		return true;
	}
	return false;
}

bool BUniform::get(int pos, int& value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Int)
	{
		int* data = (int*)member.value.data();
		value = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, float& value)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Float)
	{
		float* data = (float*)member.value.data();
		value = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BVector& vector)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec3f)
	{
		BVector* data = (BVector*)member.value.data();
		vector = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BColorf& color)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Vec4f)
	{
		BColorf* data = (BColorf*)member.value.data();
		color = data[pos];
		return true;
	}
	return false;
}
bool BUniform::get(int pos, BMatrix& matrix)
{
	if (pos < 0 || pos >= member.size)
		return false;
	if (member.type == Type_Matrixf)
	{
		BMatrix* data = (BMatrix*)member.value.data();
		matrix = data[pos];
		return true;
	}
	return false;
}
